﻿using BCCommon;
using BCCommon.Enums;
using BCData.Common.Service.WebsiteServiceType;
using BCDto.Common.Service.WebsiteServiceType;
using BCEntity.Common.Service.WebsiteServiceType;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCDto.Sim.Admin.Admin;

namespace BCService.Common.Service.WebsiteServiceType
{
    public class WebsiteServiceTypeService : IWebsiteServiceTypeService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IWebsiteServiceTypeData websiteServiceTypeData;
        private readonly string key = string.Format("{0}", EntityNameConst.WebsiteServiceTypeEntity);

        public WebsiteServiceTypeService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IWebsiteServiceTypeData websiteServiceTypeData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.websiteServiceTypeData = websiteServiceTypeData;
        }

        #region 检查官网服务类型是否存在
        public bool Exists(int websiteServiceTypeId)
        {
            return this.websiteServiceTypeData.Exists(websiteServiceTypeId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加官网服务类型
        public WebsiteServiceTypeDto Add(WebsiteServiceTypeRequestDto requestDto,AdminDto adminDto)
        {
            var entity = requestDto.As<WebsiteServiceTypeEntity>();
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;

            return this.websiteServiceTypeData.Add(entity).GetAwaiter().GetResult().As<WebsiteServiceTypeDto>();
        }
        #endregion

        #region 修改官网服务类型
        public WebsiteServiceTypeDto Update(WebsiteServiceTypePutDto putDto, AdminDto adminDto)
        {
            if (!this.Exists(putDto.WebsiteServiceTypeId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var entity = putDto.As<WebsiteServiceTypeEntity>();
            entity.EditAdminName = adminDto.FullName;
            entity.EditAdminId = adminDto.AdminId;
            var result = this.websiteServiceTypeData.Update(entity).GetAwaiter().GetResult().As<WebsiteServiceTypeDto>();
            string cacheKey = this.redisService.GetKey(key, entity.WebsiteServiceTypeId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 删除官网服务类型
        public bool Delete(int websiteServiceTypeId)
        {
            if (!this.Exists(websiteServiceTypeId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.websiteServiceTypeData.Delete(websiteServiceTypeId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteServiceTypeId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取官网服务类型
        public WebsiteServiceTypeDto Get(int websiteServiceTypeId)
        {
            string cacheKey = this.redisService.GetKey(key, websiteServiceTypeId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteServiceTypeData.GetEntity(websiteServiceTypeId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<WebsiteServiceTypeDto>();
        }
        #endregion

        #region 获取官网服务类型列表
        public IEnumerable<WebsiteServiceTypeDto> GetList()
        {
            var result = this.redisService.TryGet(key, () =>
            {
                return this.websiteServiceTypeData.GetList().GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<WebsiteServiceTypeDto>>();
        }
        #endregion

        #region 获取官网服务类型分页
        public PageableList<WebsiteServiceTypeDto> Query(string serviceTypeName, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, serviceTypeName,start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.websiteServiceTypeData.Query(serviceTypeName,start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<WebsiteServiceTypeDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<WebsiteServiceTypeDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion

        #region 启用官网服务类型
        public bool Enabled(int websiteServiceTypeId)
        {
            if (!this.Exists(websiteServiceTypeId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteServiceTypeData.Enabled(websiteServiceTypeId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteServiceTypeId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 禁用官网服务类型
        public bool Disabled(int websiteServiceTypeId)
        {
            if (!this.Exists(websiteServiceTypeId))
            {
                throw new ArgumentException("此记录不存在!");
            }
            var result = this.websiteServiceTypeData.Disabled(websiteServiceTypeId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, websiteServiceTypeId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion
    }
}